package com.stackfarm.fvservice.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stackfarm.fvcommon.pojo.Icode;
import com.stackfarm.fvcommon.pojo.Users;
import com.stackfarm.fvcommon.result.ResultBean;
import com.stackfarm.fvcommon.utils.EncryptDecryptUtil;
import com.stackfarm.fvcommon.utils.JwtUtil;
import com.stackfarm.fvcommon.utils.UserUtils;
import com.stackfarm.fvservice.mapper.IcodeMapper;
import com.stackfarm.fvservice.mapper.UsersMapper;
import com.stackfarm.fvservice.service.user.UsersService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    IcodeMapper icodeMapper;
    @Override
    public ResultBean<?> register(String username, String password,String icode) {
        ResultBean<Void> resultBean = new ResultBean<>();
        String salt = UserUtils.getSalt();
        resultBean.setData(null);
        resultBean.setTimestamp(System.currentTimeMillis());
        Users user1 = usersMapper.selectOne(new QueryWrapper<Users>().eq("username", username));
        Icode icode1=icodeMapper.selectOne(new QueryWrapper<Icode>().eq("data",icode));
        if (!UserUtils.UserIsValidate(username)) {
            resultBean.setCode(HttpStatus.BAD_REQUEST.value());
            resultBean.setMsg("用户名只能包括数字字母的组合，长度为4-15位");
        } else if (!UserUtils.PasswordIsValidate(password)) {
            resultBean.setCode(HttpStatus.BAD_REQUEST.value());
            resultBean.setMsg("密码最少8位,最多16位, 1个小写字母,1个数字和1个特殊字符");
        } else if (user1 != null) {
            resultBean.setCode(HttpStatus.BAD_REQUEST.value());
            resultBean.setMsg("此用户已经存在");
        }else if (icode1==null){
            resultBean.setCode(HttpStatus.BAD_REQUEST.value());
            resultBean.setMsg("邀请码错误");
        }
        else if (icode1.getIfused()=='1'){
            resultBean.setCode(HttpStatus.BAD_REQUEST.value());
            resultBean.setMsg("邀请码已被使用");
        }
        else {
            password = EncryptDecryptUtil.md5(password, salt);
            Users user = new Users();
            user.setUsername(username);
            user.setPassword(password);
            user.setSalt(salt);
            user.setCreatedate(new Date());
            usersMapper.insert(user);
            icode1.setIfused('1');
            icodeMapper.update(icode1,new QueryWrapper<Icode>().eq("data",icode1.getData()));
            resultBean.setMsg("注册成功");
            resultBean.setCode(HttpStatus.OK.value());
        }
        return resultBean;
    }

    @Override
    public ResultBean<Token> login(String username, String password) {
        ResultBean<Token> resultBean = new ResultBean<>();
        resultBean.setData(null);
        resultBean.setTimestamp(System.currentTimeMillis());
        Users users = usersMapper.selectOne(new QueryWrapper<Users>().eq("username", username));
        if (users == null) {
            resultBean.setMsg("用户未注册!");
            resultBean.setCode(HttpStatus.BAD_REQUEST.value());
        } else if (!users.getPassword().equals(EncryptDecryptUtil.md5(password, users.getSalt()))) {
            resultBean.setCode(HttpStatus.UNAUTHORIZED.value());
            resultBean.setMsg("密码错误");
        } else {
            resultBean.setMsg("登录成功");
            resultBean.setCode(HttpStatus.OK.value());
            resultBean.setData(new Token(JwtUtil.generateToken(username, password)));
        }
        //}
        return resultBean;
    }

    @Override
    public ResultBean<?> logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        ResultBean<Void> resultBean=new ResultBean<>();
        resultBean.setData(null);
        resultBean.setMsg("退出成功");
        resultBean.setTimestamp(System.currentTimeMillis());
        resultBean.setCode(200);
        return resultBean;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Token {
        String token;
    }
}
